package com.neuedu.web.servlet.user; /**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-19 8:51
 * 描述 : ${dc}
 */

import com.neuedu.web.dao.UserDao;
import com.neuedu.web.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

/**
 * http://localhost:8080/web/userEdit?id=15
 */
@WebServlet(name = "UserEditPageServlet", value = "/userEdit")
public class UserEditPageServlet extends HttpServlet {
    UserDao userDao = new UserDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        

        try {
            String id = request.getParameter("id");
            User user = userDao.selectOne(Integer.parseInt(id));

            //设置响应格式 为html
            response.setContentType("text/html;charset=utf8");
            // 设置响应编码
            response.setCharacterEncoding("utf-8");


            StringBuilder html = new StringBuilder();

            //action属性表示提交的路径
            html.append("  <h3>用户添加</h3>														");
            html.append("  	<form method='post' action='userUpdate'>                                          ");
            html.append("  	<input type='hidden' name='id' value='"+user.getId()+"' >	    ");
            html.append("  		<table width='500' border='1' cellpadding='0' cellspacing='0'>      ");

            html.append("  			<tr>                                                            ");
            html.append("  				<td>用户名</td>                                             ");
            html.append("  				<td><input type='text' name='username' value='"+user.getUsername()+"'></td>                ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  					<td>密码</td>                                           ");
            html.append("  				<td><input type='password' name='password' value='"+user.getPassword()+"' ></td>                ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  				<td>昵称</td>                                               ");
            html.append("  				<td><input type='text' name='displayname' value='"+user.getDisplayname()+"' ></td>             ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  					<td>birthday</td>                                       ");
            String strBirthday = "";
            if(user.getBirthday()!= null){
                strBirthday = new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday());
            }
            html.append("  				<td><input type='text' name='birthday' value='"+strBirthday+"' ></td>                ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  				<td>性别</td>                                               ");
            html.append("  				<td><input type='text' name='gender' value='"+user.getGender()+"' ></td>                  ");
            html.append("  			</tr>                                                           ");
            html.append("                                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  				<td>email</td>                                              ");
            html.append("  				<td><input type='text' name='email' value='"+user.getEmail()+"' ></td>                   ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  				<td>telephone</td>                                          ");
            html.append("  				<td><input type='text' name='telephone' value='"+user.getTelephone()+"' ></td>               ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  				<td>lastLogintime</td>                                      ");
            String strLastLogin = "";
            if(user.getLastLogintime()!= null){
                strLastLogin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getLastLogintime());
            }

            html.append("  				<td><input type='text' name='lastLogintime' value='"+ strLastLogin+"' ></td>           ");
            html.append("  			</tr>                                                           ");
            html.append("  			<tr>                                                            ");
            html.append("  			<td colspan='2' align='center'>    <input type='submit' value='提交'>   <input type='reset'></td>                                    ");
            html.append("  			</tr>                                                           ");
            html.append("  		</table>                                                            ");
            html.append("                                                                           ");
            html.append("  	</form>                                                                ");

            PrintWriter out = response.getWriter();
            out.write(html.toString());
            out.flush();
            out.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
}
